package org.apache.thrift;

import java.util.Map;

import org.apache.thrift.protocol.TMessage;
import org.apache.thrift.protocol.TMessageType;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolUtil;
import org.apache.thrift.protocol.TType;

public abstract class TBaseProcessor<I> implements TProcessor {
	private final I iface;
	private final Map<String, ProcessFunction<I, ? extends TBase>> processMap;

	protected TBaseProcessor(I iface,
			Map<String, ProcessFunction<I, ? extends TBase>> processFunctionMap) {
		this.iface = iface;
		this.processMap = processFunctionMap;
	}

	public I getIFace() {
		return iface;
	}

	public ProcessFunction<I, ? extends TBase> queryProcessFunction(String name) {
		if (this.processMap == null)
			return null;
		return this.processMap.get(name);
	}

	@Override
	public boolean process(TProtocol in, TProtocol out) throws TException {
		TMessage msg = in.readMessageBegin();
		ProcessFunction fn = processMap.get(msg.name);
		if (fn == null) {
			TProtocolUtil.skip(in, TType.STRUCT);
			in.readMessageEnd();
			TApplicationException x = new TApplicationException(
					TApplicationException.UNKNOWN_METHOD,
					getClass().getName()+" invalid method name: '" + msg.name + "'");
			out.writeMessageBegin(new TMessage(msg.name,
					TMessageType.EXCEPTION, msg.seqid));
			x.write(out);
			out.writeMessageEnd();
			out.getTransport().flush();
			return true;
		}
		fn.process(msg.seqid, in, out, iface);
		return true;
	}
}
